Procedures (Subs and Functions)

Objectives


Discussion

Procedures (Subs and Functions)

Example of a Sub

Private Sub DisplaySum(ByVal num1 As Single, ByVal num2 As Single) 'header 
   Dim answer As Single
   answer = num1 + num2
   MessageBox.Show("The answer is " & answer)
End Sub

Example of a Function

Private Function GetSum(ByVal num1 As Single, ByVal num2 As Single) As Single 'header
   Dim answer As Single
   answer = num1 + num2
   return(answer)
End Function

Calling Subs and Functions

DisplaySum(345.3, 44.55)
Dim answer as Single
answer = GetSum(12.3, 22)

ByVal versus ByRef

Back to top


Demonstration

1.  Create a new project for Unit 3.  In the code window for the form add the following code to define a sub and function.  Make sure your code is inside the class but outside of other subs and functions.  Type in the code but do NOT type in the "byval" code. After you are done you should notice that VS added "byval" for you.  Be careful, the header for each procedure should be on one line.  Depending on your text size and browser window, the header may be on two lines.

Private Sub DisplaySum(ByVal num1 As Single, ByVal num2 As Single)
   Dim answer As Single
   answer = num1 + num2
   MessageBox.Show("The answer is " & answer)
End Sub

Private Function GetSum(ByVal num1 As Single, ByVal num2 As Single) As Single
   Dim answer As Single
   answer = num1 + num2
   Return (answer)
End Function

2.  Add a button (btnCall).  We are now going to call  the sub and function. Inside the click event for the button, type the following:

displaysum(

3.  Notice that VS prompts you for information.  Because DisplaySum was defined so that it accepts two parameters, you must pass in two parameters.

4. Now type in two numbers (separated by a comma) and test your program.

5.  Now add the following code to the click event:

Dim x As Single = 1000
Dim y As Single = 12.34
DisplaySum(x, y)
DisplaySum(1234.56, x)
DisplaySum(3.4, 5.5)
Dim answer As Single
answer = GetSum(33.4, 111)
MessageBox.Show(answer)
MessageBox.Show(GetSum(x, y))

6. Run the program.  Observe the values. Notice the different ways we can call the sub and function.  Notice the last line calls the function inside the messagebox.  In this situation, the messagebox is expecting something to display; the function is returning that something based on what was passed into the function.

7.  Now we will explore ByVal and ByRef with a very simple sub.  We could have just as easily used a function for this example. Create the following Sub in the code window

Private Sub ChangeIt(ByVal iByVal As Int16, ByRef iByRef As Int16)
   iByVal = iByVal * 2
   iByRef = iByRef * 2
   MessageBox.Show("INSIDE: iByVal = " & iByVal & " iByRef = " & iByRef)
End Sub

8.  Now add another button (btnBy) and add the following code to the click event:

Dim iV As Int16 = 100
Dim iR As Int16 = 100
MessageBox.Show("BEFORE: iV = " & iV & " iR = " & iR)
ChangeIt(iV, iR)
MessageBox.Show("AFTER: iV = " & iV & " iR = " & iR)

9.  Look carefully at the code.  Notice that iV is being passed into iByVal and iR is being passed into iByRef and that iByVal is ByVal and iByRef is ByRef. Run the code and observe the values of the parameters.  Notice that iV has not changed after the sub was called but that iR has changed.

10.  This demonstration showed the difference between byref and byval.Remember that VS makes a parameter byval by default and so you may never have to think about this.  Also you will probably never need to use byref, but you never know.

Back to top
Exercises

1.  Write a function that accepts two strings (sFirstName and sLastName) and returns a string with the fullname.  Make sure you include a space between the names.

2.  Write sub that accepts two strings (sFirstName and sLastName) and displays the fullname in a messagebox. 

3.  Write a function that accepts the radius and returns the area of a circle with that radius.  The area of a circle is PI*r^2.

4.  Write a sub that accepts the length and width of a rectangle and displays the perimeter in a messagebox. The perimeter is 2*(w+l).

5.  Write a program to test your functions and subs in #1 to 4.

6.  The following function accepts the mass and velocity of an object and returns the kinetic energy of it.  Write a single line of code that will pass in 100 for mass and 22 for velocity and display the result in a messagebox.

Private Function CalcEnergy(ByVal nMass As Single, ByVal nVelocity As Single) As Single 
   return(0.5*nMass*nVelocity*nVelocity)
End Function

Back to top
Links & Help
Back to top